package com.googlecode.xapper

import static org.hamcrest.Matchers.*
import static org.junit.Assert.*

import org.junit.*

import nu.xom.*


public class TestGenerationOfAHtmlPage{

	def context = new XPathContext("html", "http://www.w3.org/1999/xhtml");

	def html
	
	@Before
	public void setupDocument() {
		html = HtmlElementWrapper.create("html")
			.withChild("head")
			.withChild("title")
			.withText("Xapper: Hello World")
			.parent().parent()
			.withChild("body")
			.withHeading(1, "Hello World!")
			.withSibling("p")
			.withText("Created by Xapper!")
			.root()
			.asElement()

	}
	
	@Test
	public void shouldHaveAHtmlWrapper() {
		assertThat(html.query("/html:html", context).size(), is(1))
	}
	
	@Test
	public void shouldGenerateHaveAHead() {
		//println(html.toXML())
		assertThat(html.query("/html:html/html:head", context).size(), is(1))
	}
	
	@Test
	public void shouldHaveABody() {
		assertThat(html.query("/html:html/html:body", context).size(), is(1))
	}
	
	@Test
	public void shouldHaveAParagraphInTheBody() {
		def nodes = html.query("//html:body/html:p", context)
		
		assertThat(nodes.size(), is(1))
	}
	
	@Test
	public void shouldHaveATitle() {
		assertThat(html.query("//html:head/html:title", context).size(), is(1))
	}
	
	@Test
	public void shouldHaveTextInTheTitle() {
		assertThat( html.query("//html:title", context).get(0).getValue(),
				is(equalTo("Xapper: Hello World")))
	}
	
	@Test
	public void shouldHaveTextInTheParagraph() {
		assertThat(html.query("//html:p", context).get(0).getValue(),
				is(equalTo("Created by Xapper!")))
	}
}
